<?php

$hostname='localhost';
$username='root';
$password='mysql100%';

$link=mysql_connect($hostname,$username,$password);

$status=mysql_select_db("vtigercrm520");

$query="SELECT contactid , cf_614 FROM vtiger_contactscf WHERE cf_614 IS NOT NULL";

$firstquery=mysql_query($query, $link);

if(!$firstquery) {
		print("Query Error: ".mysql_error());
	}

   while($contactdata=mysql_fetch_assoc($firstquery)) {
   print("fetching scf <br>");
   $currentcid=$contactdata['contactid'];
   $currentKontaktCompanyID = $contactdata['cf_614'];
   $accountscfquery="SELECT accountid FROM vtiger_accountscf WHERE cf_662 = $currentKontaktCompanyID";
   $secondq=mysql_query($accountscfquery, $link);
   if(!$secondq) {
		print("Query Error: ".mysql_error());
	}
	$accountscfdata=mysql_fetch_assoc($secondq);
	$accountid=$accountscfdata['accountid'];
	print("accountid is for cid $currentcid with KontaktID $currentKontaktCompanyID is: $accountid <br>");
	print("performing update... <br>");
	$updatequery="UPDATE vtiger_contactdetails SET accountid = $accountid WHERE contactid = $currentcid";
	$status=mysql_query($updatequery, $link);
	if (!status) {
		print("Query error:" .mysql_error());	
	}
};
mysql_close($link);
echo "Everything done.<br>"
?>
